package com.hd.crdao.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.CheckUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 * Created by Programming on 2017/6/15 0015.
 * 修改推荐人号码
 */
public class ChangeRecommandGuyActivity extends BaseSecondActivity {


    @BindView(R.id.changerecommandguy_head)
    ExpandHeadView mChangerecommandguyHead;
    @BindView(R.id.recommandPhone)
    EditText mRecommandPhone;
    @BindView(R.id.changerecommandguy_tip)
    TextView mChangerecommandguyTip;
    @BindView(R.id.changerecommandguy_submit)
    Button mChangerecommandguySubmit;

    @Override
    protected void initView() {
        initData();
        mChangerecommandguySubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSave();
            }
        });
    }

    private void initData() {
        String recomMobile = SPUtils.getString("recomMobile", "");
        if (!TextUtils.isEmpty(recomMobile)) {
            mRecommandPhone.setText(recomMobile);
            mChangerecommandguyTip.setVisibility(View.GONE);
            mChangerecommandguySubmit.setVisibility(View.GONE);
            mRecommandPhone.clearFocus();
            mRecommandPhone.setFocusableInTouchMode(false);
        } else {
            mChangerecommandguyTip.setVisibility(View.VISIBLE);
            mChangerecommandguySubmit.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 修改推荐人
     */
    private void onSave() {
         final String recomMobile = mRecommandPhone.getText().toString().trim();
        if (TextUtils.isEmpty(recomMobile)) {
            MyToast.show("请填写推荐人号码");
            return;
        } else if (recomMobile.length() != 11 | !CheckUtils.isMobileNO(recomMobile)) {
            MyToast.show("输入的手机号不正确");
            return;
        }
        OkHttpUtils.post().url(Constants.UPDATERECOMMOBILE_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("recomMobile", recomMobile)
                .addParams("carBusinessId", SPUtils.getString(Config.businessId, ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    MyToast.show("修改成功");
                    SPUtils.saveString("recomMobile", recomMobile);
                    initData();
                } else {
                    MyToast.show(msgBean.getMessage());
                }
            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_changerecommandguy;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mChangerecommandguyHead;
    }


}
